<?php
/**
 * Created by PhpStorm.
 * User: liuchaozhong
 * Date: 2018/8/2
 * Time: 下午6:18
 */
namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Article extends Model
{
    protected $table = 'article';

    //状态
    public $status_arr = [
        '待审核', '已审核' , '审核不过'
    ];


    //文章分类关系
    public function get_cate()
    {
        return $this->belongsTo(Category::class, 'cate_id');
    }

    //文章关联收藏
    public function g_collect()
    {
        return $this->hasOne(Collect::class, 'article_id');
    }

    /**
     * 获取分类名称
     * @param $cate_id
     */
    public function getCateName($cate_id = '')
    {
        if(is_numeric($cate_id) && $cate_id > 0){
            return Category::find($cate_id);
        }
        
    }

    //批量审核
    public function check($ids, $type)
    {
        if($type == 1){
            $update = ['status'=>1];
        }elseif($type == 0){
            $update = ['status'=>0];
        }
        try {
            $res = Article::whereIn('id',$ids)->update($update);
        }catch (\Exception $e) {
            $res = false;
        }

        return $res ? $res : false;
    }

}